package ntd.util.frame.mybatis.plus.generator;

import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import ntd.util.config.common.controller.BaseMybatisController;
import ntd.util.config.common.entity.BaseEntityMybatis;
import ntd.util.frame.data.factory.FactoryData;

import java.util.function.Consumer;

public class GeneratorCode {

    public static void main(String[] args) {
        codeGenerator("things", "things_device_data");
    }

    private static void codeGenerator(String moduleName, String tableName) {

        FastAutoGenerator.create(new DataSourceConfig.Builder(FactoryData.dataSource()))
                .globalConfig(builder -> {
                    builder.author("liuwenju") // 设置作者
                            // 开启 swagger 模式
                            .enableSwagger()
                            // 覆盖已生成文件
                            .fileOverride()
                            .disableOpenDir()
                            // 指定输出目录
                            .outputDir(System.getProperty("user.dir") + "/src/main/java");
                })
                .packageConfig(builder -> {
                    builder.parent("ntd.util.modular") // 设置父包名
                            .moduleName(moduleName) // 设置父包模块名
                            // 设置mapperXml生成路径
//                            .pathInfo(Collections.singletonMap(OutputFile.mapperXml, "D://work/work-generator-code"))
                    ;
                })
                .strategyConfig(strategyConfig(moduleName, tableName))
                // 使用Freemarker引擎模板，默认的是Velocity引擎模板
//                .templateEngine(new FreemarkerTemplateEngine())
                .execute();
    }

    private static Consumer<StrategyConfig.Builder> strategyConfig(String moduleName, String tableName) {
        return builder -> {
            // 设置需要生成的表名
            builder.addInclude(tableName);
            // 设置过滤表前缀
            builder.addTablePrefix(moduleName);
            builder.enableCapitalMode();
            builder.enableSkipView();
            builder.disableSqlFilter();

            // entity 配置
            builder.entityBuilder()
                    .superClass(BaseEntityMybatis.class)
                    .enableChainModel()
                    .enableLombok()
                    .enableRemoveIsPrefix()
                    .enableTableFieldAnnotation()
                    .addSuperEntityColumns("id", "created_date", "created_by", "updated_date", "updated_by")
            ;

            // controller 配置
            builder.controllerBuilder()
                    .superClass(BaseMybatisController.class)
                    .enableHyphenStyle()
                    .enableRestStyle()
            ;
        };
    }
}
